﻿using Enter.Admin.Utils;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace Enter.Admin.Models.DataTables
{
    public class DataTablesColumn
    {
        //data = t.PropertyName, title = t.HeaderName, orderable = false, visible = true, searchable = false
        public string data { get; set; }
        public string title { get; set; }
        public string dataType { get; set; }
        // 是否可以排序
        public bool orderable { get; set; }
        // 是否显示
        public bool visible { get; set; }
        // 是否可以搜索
        public bool searchable { get; set; }
        public string drop { get; set; }
        // 是否导出
        public bool export { get; set; } = true;
        // 是否导入
        public bool import { get; set; } = true;
    }

    public class DataTablesColumnExt
    {
        public static ConcurrentDictionary<string, List<DataTablesColumn>> ColumnMap = new ConcurrentDictionary<string, List<DataTablesColumn>>();
        public static List<DataTablesColumn> ToList<T>() 
        {
            var key = typeof(T).FullName;
            return ColumnMap.GetOrAdd(key, (key) => {
                var columns = Excel.Headers<T>();
                return columns.Select(t => new DataTablesColumn { 
                    data = t.PropertyName, 
                    title = t.HeaderName, 
                    orderable = t.Orderable, 
                    visible = t.Visible, 
                    searchable = t.Searchable, 
                    dataType = t.Property.PropertyType.Name,
                    drop = t.Property.PropertyType.IsEnum? t.Property.PropertyType.Name: t.Drop,
                    import = t.Import,
                    export = t.Export,
                }).ToList();
            });
        }
        
    }
}
